<!-- 修改密码 -->
<script setup>
import { ref } from 'vue'

const formRef = ref()
const pwdForm = ref({
  old_pwd: '',
  new_pwd: '',
  re_pwd: ''
})

const checkDifferent = (rule, value, callback) => {
  // 校验新密码和原密码不能一样
  if (value === pwdForm.value.old_pwd) {
    callback(new Error('新密码不能与原密码一样'))
  } else {
    callback()
  }
}
const checkSameAsNewPwd = (rule, value, callback) => {
  // 校验确认密码必须和新密码一样
  if (value !== pwdForm.value.new_pwd) {
    callback(new Error('确认密码必须和新密码一样'))
  } else {
    callback()
  }
}
const rules = ref({
  old_pwd: [
    { required: true, message: '请输入原密码', trigger: 'blur' },
    { min: 6, max: 15, message: '原密码长度在6-15位之间', trigger: 'blur' }
  ],
  new_pwd: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, max: 15, message: '新密码长度在6-15位之间', trigger: 'blur' },
    { validator: checkDifferent, trigger: 'blur' }
  ],
  re_pwd: [
    { required: true, message: '请再次输入新密码', trigger: 'blur' },
    { min: 6, max: 15, message: '确认密码长度在6-15位之间', trigger: 'blur' },
    { validator: checkSameAsNewPwd, trigger: 'blur' }
  ]
})

// const router = useRouter()

// const submitForm = async () => {
//   await formRef.value.validate()
//   await userUpdatePasswordService(pwdForm.value)
//   ElMessage.success('密码修改成功')

//   // 密码修改成功后，退出重新登录
//   // 清空本地存储的 token 和 个人信息
//   userStore.setToken('')
//   userStore.setUser({})

//   // 拦截登录
//   router.push('/')
// }

//将数据进行清除，重置
const resetForm = (formRef) => {
  if (!formRef) return
  formRef.resetFields()
}
</script>

<template>
  <div class="my-password">
    <el-card style="width: 50rem; height: 21.875rem; margin-top: 8.125rem; opacity: 0.95">
      <el-form
        :rules="rules"
        label-width="6.25rem"
        style="margin-top: 4.375rem"
        ref="formRef"
        :model="pwdForm"
      >
        <el-form-item label="原密码" prop="old_pwd">
          <el-input v-model="pwdForm.old_pwd" show-password></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="new_pwd">
          <el-input v-model="pwdForm.new_pwd" show-password></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="re_pwd">
          <el-input v-model="pwdForm.re_pwd" show-password></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary">修改密码</el-button>
          <el-button @click="resetForm(formRef)">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<style lang="scss" scoped>
.my-password {
  // background: url('@/assets/images/学习.jpg') no-repeat 60% center / 245px auto;
  margin: 0px;
  background: url(@/assets/images/学习.jpg) no-repeat;
  background-size: 100% 100%;
  // background-attachment: fixed;
  width: 100%;
  height: 100%;
  position: absolute;
  // z-index: -1;
  display: flex;
  justify-content: center;
  // align-items: center;
}
</style>
